//停止搜寻附近的蓝牙外围设备
export default {
	//停止搜寻附近的蓝牙外围设备
	stopFindBlue() {
		uni.stopBluetoothDevicesDiscovery({
			success(res) {}
		})
	},
	// 自动连接蓝牙适配器
	bluetoothAdapter() {
		return new Promise((resolve, reject) => {
			const BluetoothAdapter = plus.android.importClass(
				'android.bluetooth.BluetoothAdapter'); // 引入Java 蓝牙类
			const blueadapter = BluetoothAdapter.getDefaultAdapter(); //拿到默认蓝牙适配器方法
			if (blueadapter) {
				// 判断蓝牙是否开启
				if (!blueadapter.isEnabled()) {
					// 未开启弹出提示框
					uni.showModal({
						title: '提示',
						content: '蓝牙尚未打开，是否打开蓝牙',
						showCancel: true,
						cancelText: '取消',
						confirmText: '确定',
						success(res) {
							// 点击确定后通过系统打开蓝牙
							if (res.confirm) {
								const blueadapter = BluetoothAdapter
									.getDefaultAdapter();
								if (blueadapter != null) {
									blueadapter.enable();
									setTimeout(() => {
										resolve()
									}, 2000)
								}
							}
						}
					})
				}
			}
		})
	},
	// 搜索蓝牙设备
	openBluetoothAdapter() {
		return new Promise((resolve, reject) => {
			let that = this
			uni.openBluetoothAdapter({
				success(res) {
					uni.getBluetoothAdapterState({
						success: function(res) {
							// 蓝牙适配器是否可用
							if (res.available) {
								uni.startBluetoothDevicesDiscovery({
									success(val) {
										resolve()
									}
								})
								// 是否正在搜索设备
								if (res.discovering) {
									// that.stopFindBlue()
								}
							} else {}
						},
					})
				},
				fail: function(res) {
					reject(res)
				}
			})
		})
	},
	// 设置关闭蓝牙后,已连接的蓝牙都失效
	closeBluetoothAdapter() {
		uni.closeBluetoothAdapter({
			success(res) {
				let arr = uni.getStorageSync('blueToothConnectedDevices')
				for (var i = 0; i < arr.length; i++) {
					arr[i].sufText = '未连接'
				}
				uni.setStorageSync('blueToothConnectedDevices', arr)
				uni.removeStorageSync('blueToothDeviceId')
				uni.removeStorageSync("blueToothServiceId");
				uni.removeStorageSync("blueToothCharacteristicId");
			}
		})
	},
	// 连接蓝牙
	createBLEConnection(e) {
		return new Promise((resolve, reject) => {
			let that = this
			uni.createBLEConnection({
				deviceId: e.item.deviceId,
				success: res => {
					resolve()
					that.stopFindBlue()
				},
				fail: res => {
					uni.showToast({
						title: '连接超时',
						icon: 'error',
						duration: 2000
					})
				},
				complete(res) { //无论有没有连上蓝牙，都要停止搜索
					that.stopFindBlue(); //停止搜索蓝牙
				}
			})
		})
	},
	//获取已经连接的蓝牙设备的所有服务
	getBLEServices(e) {
		return new Promise((resolve, reject) => {
			var that = this;
			setTimeout(() => {
				uni.getBLEDeviceServices({
					// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
					deviceId: e.item.deviceId,
					success: (res) => {
						let serviceId = ""
						if (res.services.length == 0) {
							uni.hideNavigationBarLoading() //关闭加载动画
						}
						for (var s = 0; s < res.services.length; s++) {
							let serviceId = res.services[s].uuid
							uni.getBLEDeviceCharacteristics({
								// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
								deviceId: e.item.deviceId,
								// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
								serviceId: serviceId,
								success: (res) => {
									var re = JSON.parse(JSON.stringify(res))
									for (var c = 0; c < re.characteristics
										.length; c++) {
										if (re.characteristics[c].properties
											.write ==
											true) {
											let uuid = re.characteristics[c]
												.uuid
											let connectedDevices = []
											connectedDevices.push({
												text: e.item.text,
												deviceId: e.item
													.deviceId,
												services: [],
												sufIcon: "iconfont icon-Rrl_s_074",
												sufText: '已连接',
												sufIconStyle: 'color:#409EFE;font-size:60rpx',
												serviceId: serviceId,
												characteristicId: uuid
											})
											resolve(connectedDevices)
											// for (var index in that.devices) {
											// 	if (that.devices[index].deviceId == deviceId) {
											uni.setStorageSync(
												"blueToothDeviceId", e.item
												.deviceId
											) //把已经连接的蓝牙设备信息放入缓存
											uni.setStorageSync(
												"blueToothCharacteristicId",
												uuid) //把已经连接的蓝牙设备标识放入缓存
											uni.setStorageSync(
												"blueToothServiceId",
												serviceId
											) //把已经连接的蓝牙设备ID放入缓存
											uni.showToast({
												title: '连接蓝牙成功',
												icon: 'success',
												duration: 2000
											})
											// that.devices[index].services.push({
											// 	serviceId: serviceId,
											// 	characteristicId: uuid
											// })
											break
											// 	}
											// }
										}
									}
								}
							})
						}
					},
					fail(res) {
						uni.showToast({
							title: '连接失败',
							icon: 'none',
							duration: 2000
						})
					},
				})
			}, 5000)
		})
	}
}